﻿using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using DataBaseConnect.App.RoleApp;
using Entity;
using Utils;
using DataBaseConnect;
using DataBaseConnect.App.MenuApp;
using Entity.Dto;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class RoleController : BaseController
    {
        #region 构造实例化

        public IRoleAppService _roleAppService;
        public IMenuAppService _menuAppService;
        public IRoleRepository _roleRepository;

        public RoleController(IRoleAppService roleAppService, IMenuAppService menuAppService, IRoleRepository roleRepository)
        {
            _roleAppService = roleAppService;
            _menuAppService = menuAppService;
            _roleRepository = roleRepository;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <returns></returns>
        public IActionResult AddRole()
        {
            return View();
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public IActionResult EditRole(int RoleId)
        {
            var model = _roleAppService.GetRoleFirst(RoleId);
            return View(model);
        }

        /// <summary>
        /// 获取角色对应得菜单权限
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public IActionResult MenuRole()
        {
            return View();
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRoleList(string RoleName)
        {
            ReceiveStatus<Role> receiveStatus = new ReceiveStatus<Role>();
            List<Role> list = new List<Role>();
            try
            {
                //得到所有菜单
                list = _roleAppService.GetRoleList(RoleName);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 新增角色信息&&查询
        /// </summary>
        /// <param name="Input">角色信息实体</param>
        /// <returns></returns>
        public JsonResult AddRoleMsg(Role Input)
        {
            return Json(_roleAppService.AddRoleMsg(Input));
        }

        /// <summary>
        ///  编辑角色信息
        /// </summary>
        /// <param name="Input">角色信息实体</param>
        /// <returns></returns>
        public JsonResult EditRoleMsg(Role Input)
        {
            return Json(_roleAppService.EditRoleMsg(Input));
        }

        /// <summary>
        /// 根据主键删除角色信息
        /// </summary>
        /// <param name="RoleId">角色信息集合</param>
        /// <returns></returns>
        public JsonResult DelRoleMsg(List<Role> RoleId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            foreach (var item in RoleId)
            {
                receiveStatus = _roleAppService.DelRoleMsg(item.RoleId);
            }
            return Json(receiveStatus);
        }

        /// <summary>
        /// 获取角色对应的菜单权限
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllMenuList(int RoleId)
        {
            List<MenuDto> menuTree = new List<MenuDto>();
            var role = _menuAppService.GetRoleIdList(RoleId);
            var menu = _menuAppService.GetAllMenuList();
            foreach (var model in menu)
            {
                menuTree.Add(new MenuDto {
                    MenuId=model.MenuId,
                    MenuName = model.MenuName,
                    MenuParentId = model.MenuParentId,
                    MenuUrl = model.MenuUrl,
                    Checked = false
                });
            }
            foreach (var item in role)
            {
                foreach (var data in menuTree)
                {
                    if (item.MenuId == data.MenuId)
                    {
                        data.Checked = true;
                    }
                }
            }
            return Json(menuTree);
        }

        /// <summary>
        /// 设置角色菜单权限
        /// </summary>
        /// <param name="MenuId">菜单id集合</param>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        public JsonResult SetMenuRoleMsg(List<MenuRole> MenuId,int RoleId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                receiveStatus= _roleRepository.DelMenuRoleMsg(f => f.RoleId == RoleId);
                if (MenuId.Count != 0)
                {
                    receiveStatus= _roleRepository.SetMenuRoleMsg(MenuId);
                }
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = "失败：" + ex.Message;
            }
           
            return Json(receiveStatus);
        }

        #endregion
    }
}
